<?php
class ArticleCateModel extends MongoModel{
	var $table ='article_cate';
	var $name = '文章分类';
	
	public static function getTree(){
		$key = 'article_cate_' . SITE_ID;
		$tree = Yii::app()->cache->get($key);
		if(true && $tree === false){
			$mod = new ArticleCateModel();
			$condition = array('site_id'=>SITE_ID);
			$cates = $mod->find($condition, 'name,parent,rank', null, null, array('rank'=>1));
			$tree = new YTree($cates);
			Yii::app()->cache->set($key, $tree);
		}
		return $tree;
	}
	
	public static function getOpts($has_null=false, $null_text = '--不限制--'){
		$tree = self::getTree();
		$opts = $tree->get_options('┊');
		if($has_null){
			return array_merge(array(''=>$null_text), $opts);
		}else{
			return $opts;
		}
	}
	
	/**
	 * 获取某分类的路径列表，从root到当前分类
	 * @param unknown $cate_id
	 */
	public static function getCateFamilyList($cate_id){
		$mod = new ArticleCateModel();
		$condition = array('site_id'=>SITE_ID);
		$cates = $mod->find($condition, 'name,parent,rank', null, null, array('rank'=>1));
		$list = array();
		if(isset($cates[$cate_id])){
			$node = $cates[$cate_id];
		}else{
			return null;
		}
		$list[] = $node;
		while(isset($node['parent'])){
			if(isset($cates[$node['parent']])){
				$node = $cates[$node['parent']];
				$list[] = $node;
			}else{
				break;			
			}
		}
		return array_reverse($list);
	}
	
	public static function cleanCache(){
		$key = 'article_cate_' . SITE_ID;
		Yii::app()->cache->delete($key);
	}
	
	public static function genUrl($article_cate){
		return '/Article/Cate/' . $article_cate['_id'];
	}
}

?>